package com.diana.common;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 多线程测试queue生成的并发
 */
public class Test {

    private Logger log = LoggerFactory.getLogger(Test.class);

    ThreadPoolExecutor pool = new ThreadPoolExecutor(10,100,60, TimeUnit.MICROSECONDS,new LinkedBlockingQueue<>());

    public void dopost(){

        String host = "http://localhost:8004";
        String path = "/queueMain/getQueueValue";
        String mothed = "post";
        Map<String,String> headers = new  HashMap<>();
        Map<String ,String> body = new HashMap<>();
        body.put("queueName","managerId");

        try {
            HttpResponse response = (HttpResponse) HttpUtils.doPost(host, path, mothed, headers, null, body);
            String res = EntityUtils.toString(response.getEntity());
            log.info(res);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }

    }

    public void thread(){
        for (int i=0;i<200;i++){
            /*try {
                Thread.sleep(500);
            }catch (Exception e){
                e.printStackTrace();
            }*/
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    dopost();
                }
            });
        }
    }

    public static void main(String[] args) {
        Test tt = new Test();
        tt.thread();
    }
}
